/**
YASTACS intro playing module.

License: GNU General Public License

YASTACS game
Copyright (C) 2012 Kalman Kiss, kiskami@freemail.hu

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Contact author at: kiskami@freemail.hu or
Hungary, 8900 Zalaegerszeg
Kakukkfu u 4.
*/
module intro.Intro;

import std.stdio;

import iCore;
import Common;

import ogreint.KeyCode;
import ogreint.Timer;
import ceguiint.Window;
import rendering.CameraYaw;

immutable string titlescreen_layout = "titlescreen.layout";

class Intro : GameState {

	this(iCore core_) {
		debug writeln("Intro ctor");
		core = core_;
		state = State.CREATED;

		timer = new Timer();

		core.rendersystem.setSkyBox("backgrounds/FirstSimpleStarField");

		// load intro and titlescreen elements
		w = core.gui.loadLayout(titlescreen_layout);

		core.gui.setWidgetText("TitleScreenRoot/version","version "~ver);
		core.gui.setWidgetText("TitleScreenRoot/copyright",copyright ~ "\n" ~ licence);
		core.gui.setWidgetText("TitleScreenRoot/msg","");//"Loading, please wait...");

		cy = new CameraYaw(core.rendersystem.getCamera()/*, speed_per_degree */);
	}

	void initialize() {
		debug writeln("Intro initialize");

		// ez lesz a guisheet
		core.gui.add(w);

		state = State.INITED;
		runstate = RunState.NONE;

		mouseshown = core.gui.showMouseCursor(false);
	}

	void update(real elapsedTime) {
//		debug writeln("Intro update");

		if(state != State.INITED && state != State.RUNNING) {
			debug writeln("Intro current state is ", state);
			throw new Exception("Intro is in invalid state to run.");
		}

		if(state == State.INITED) {
			// épphogy betöltődtünk, indulhat a móka
			state = State.RUNNING;
		}

		// esc lenyomásra ugrás
		if(core.input.keyPressed(KeyCode.KC_ESCAPE)) {
			core.toGameState(eGameStates.INTRO, eGameStates.STARTMENU);
			return;
		}

		if(runstate == RunState.NONE) {
			runstate = RunState.INTROSHOWN;
			timer.reset();
		}

		if(runstate == RunState.INTROSHOWN) {
			// intro lejátszás
			// még nincs

			// a végén
			runstate = RunState.INTROEND;
			// váltás a köv. frame-ben?
//			return;
		}

		if(runstate == RunState.INTROEND) {
			w.show();
			runstate = RunState.TITLESHOWN;
			timer.reset();
			// itt indul a kameramozgatás is
			cy.run();
		}

		if(runstate == RunState.TITLESHOWN) {

			cy.update(elapsedTime);

			// kilépés, ha már mutattuk eleget a titlescreen-t
			if(timer.getMilliseconds()>DEF_INTROTITLESCREEN_SHOWTIME*1000)
				core.toGameState(eGameStates.INTRO, eGameStates.STARTMENU);
		}
	}

	void pause() {
		cy.stop();
	}
	void restart() {
		cy.run();
	}

	void deinitialize() {
		debug writeln("Intro deinitialize");

		core.gui.showMouseCursor(mouseshown);

		cy.stop();

		w.hide();
		core.gui.remove(w);

		// unload intro and titlescreen elements
		state = State.DEINITED;
		runstate = RunState.NONE;
	}

private:

	iCore core;

	enum State {
		CREATED,
		INITED,
		RUNNING,
		DEINITED
	};

	enum RunState {
		NONE,
		INTROSHOWN,
		INTROEND,
		TITLESHOWN
	};

	State state;
	RunState runstate;

	Window w;

	Timer timer;

	bool mouseshown;

	CameraYaw cy;
};
